﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;

public partial class User_ComposeMail : System.Web.UI.Page
{
    UserMailbox MailRepo = new UserMailbox();
    UsersRepository URepo = new UsersRepository();
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Compose_Click(object sender, EventArgs e)
    {
        try
        {
            ComposeMail();
            ExtNet.Msg.Alert("Compose", "Đã gửi thư thành công");
        }
        catch
        {
        }
    }

    public void ComposeMail()
    {
        HttpCookie _user = Request.Cookies["userinfo"];
        int userId = 0;
        if (Request.Cookies["userinfo"] != null)
        {
            if (_user["username"] != null)
            {
                userId = URepo.GetUserIdByName(_user["username"]);
            }
        }
        char[] separator = new char[] { ',' };
        string[] strTo = txtReceiver.Text.Split(separator);
        string SenderEmail = URepo.GetEmailById(userId);
        if (userId > 0)
        {
            foreach (string To in strTo)
            {
                if (!String.IsNullOrEmpty(To))
                {
                    try
                    {
                        int receiverId = URepo.GetIdByEmailOrUsername(To.Trim());
                        MailRepo.ComposeMail(receiverId, txtSubject.Text, CKEditorControl1.Text, userId, (int)Mailbox.unread, (int)Mailbox.Inbox, DateTime.Now, SenderEmail, DateTime.Now,0);
                    }
                    catch
                    {
                    }
                }
            }
        }
    }
}